﻿using JshRCSWebApi.Infrastructure.Domain.DTO.SysVehicle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace JshRCSWebApi.Util.VehicelTool
{
    public class VehicleHelper
    {
        //public static byte[] seerMessageHeadToBytes(SeerMessageHead msg)
        //{
        //    var hsize = Marshal.SizeOf(msg);
        //    byte[] bytes = new byte[hsize];
        //    IntPtr structPtr = Marshal.AllocHGlobal(hsize);
        //    Marshal.StructureToPtr(msg, structPtr, false);
        //    Marshal.Copy(structPtr, bytes, 0, hsize);
        //    Marshal.FreeHGlobal(structPtr);
        //    return bytes;
        //}
        public static T bytesToStructure<T>(byte[] bytesBuffer)
        {
            if (bytesBuffer.Length < Marshal.SizeOf(typeof(T)))
            {
                throw new ArgumentException("size error");
            }

            IntPtr bufferHandler = Marshal.AllocHGlobal(bytesBuffer.Length);
            for (int index = 0; index < bytesBuffer.Length; index++)
            {
                Marshal.WriteByte(bufferHandler, index, bytesBuffer[index]);
            }
            T structObject = (T)Marshal.PtrToStructure(bufferHandler, typeof(T));
            Marshal.FreeHGlobal(bufferHandler);
            return structObject;

        }
    }
}
